//---------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Resources;
using System.Data;
using System.Threading;

namespace TaskVision
{
	[DefaultEvent("ClickItem")] 
	public class TaskHistoryPanel : System.Windows.Forms.UserControl
	{
		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private DataView m_DataView = null;
		private string m_TaskIDMember = string.Empty;
		private string m_OptionalDateMember = string.Empty;
		private string m_DisplayMember = string.Empty;
																													   
		private int m_SelectedTaskID = -1; // magic number for nothing
		private int m_MaxListItems = 0;

		private const int c_LinkLabelStartingX = 5;
		private const int c_LinkLabelStartingY = 5;
		private const int c_LinkLabelHeight = 16;

		public delegate void HistoryLinkClickedHandler(int taskID, int index);
		public event HistoryLinkClickedHandler HistoryLinkClicked;
		private ResourceManager m_ResourceManager = new ResourceManager("TaskVision.Localize", System.Reflection.Assembly.GetExecutingAssembly());

		[Browsable(false), Description("DataView used for data binding.")]
		public DataView DataSource
		{
			set
			{
				if (value == null)
					m_DataView = null;
				else
					m_DataView = value;
			}
		}

		[Browsable(false), Description("Column name of date last modified.")]
		public string OptionalDateMember
		{
			set
			{
				m_OptionalDateMember = value;
			}
		}
		
		[Browsable(false), Description("Column name of ID key.")]
		public string TaskIDMember
		{
			set
			{
				m_TaskIDMember = value;
			}
		}
		
		[Browsable(false), Description("Column name to display as text.")]
		public string DisplayMember
		{
			set 
			{
				m_DisplayMember = value;
			}
		}

		[Browsable(false), Description("Selected ID key.")]
		public int SelectedTaskID
		{
			set
			{
				m_SelectedTaskID = value;
				DataBind();
			}
			get
			{
				return m_SelectedTaskID;
			}
		}

		[Browsable(false), Description("Maximum items to list.")]
		public int MaxListItems
		{
			set
			{
				m_MaxListItems = value;
			}
		}


		private void DataBind() 
		{
			// clear all controls
			this.Controls.Clear();

			// if data binding is not setup, create a label and return
			if (m_DataView == null || m_TaskIDMember == string.Empty || m_SelectedTaskID == -1 || m_MaxListItems == 0)
			{
				Label newLabel = new Label();
				newLabel.Text = m_ResourceManager.GetString("history_unavailable");
				newLabel.Location = new System.Drawing.Point(c_LinkLabelStartingX, c_LinkLabelStartingY);
				newLabel.Size = new System.Drawing.Size((this.Width - (c_LinkLabelStartingX * 2)), c_LinkLabelHeight);
				newLabel.Name = "Label";
				newLabel.TabIndex = 0;
				newLabel.TabStop = true;
				newLabel.FlatStyle = FlatStyle.System;

				// add the label
				this.Controls.Add(newLabel);
			} 
			else 
			{
				// we have a dataview, member value, task id, and are allow to display more than 0 items
				int numLinks = 0;
				foreach (DataRowView row in m_DataView)
				{
					if ((int) row[m_TaskIDMember] == m_SelectedTaskID)
					{
						string datePrefix = string.Empty;

						if (m_OptionalDateMember != string.Empty)
						{
							DateTime dateModified = (DateTime) row[m_OptionalDateMember];

							// globalize for german
							if (Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName == "de")
								datePrefix = dateModified.ToString("dd/MM ");
							else
								datePrefix = dateModified.ToString(@"MM/dd b\y ");
						}

						// create a linklabel
						LinkLabel  newLinkLabel = new LinkLabel();
						newLinkLabel.Text = datePrefix + (string) row[m_DisplayMember];
						newLinkLabel.Location = new System.Drawing.Point(c_LinkLabelStartingX, (c_LinkLabelStartingY + (numLinks * c_LinkLabelHeight)));
						newLinkLabel.Size = new System.Drawing.Size((this.Width - (c_LinkLabelStartingX * 2)), c_LinkLabelHeight);
						newLinkLabel.Name = "LinkLabel" + numLinks;
						newLinkLabel.Tag = (numLinks);
						newLinkLabel.TabStop = true;
						newLinkLabel.FlatStyle = FlatStyle.System;

						// add the link label
						this.Controls.Add(newLinkLabel);
						newLinkLabel.Click += new System.EventHandler(this.LinkLabel_Click);

						// increment the number of matching links
						numLinks++;
					}

					// if we hit the max items
					if (numLinks == m_MaxListItems)
						break;
				}

				// if we didn't have any matching history items
				if (numLinks == 0) 
				{
					// create a label
					Label newLabel = new Label();
					newLabel.Text = m_ResourceManager.GetString("no_history");
					newLabel.Location = new System.Drawing.Point(c_LinkLabelStartingX, c_LinkLabelStartingY);
					newLabel.Size = new System.Drawing.Size((this.Width - (c_LinkLabelStartingX * 2)), c_LinkLabelHeight);
					newLabel.Name = "Label";
					newLabel.TabIndex = 0;
					newLabel.TabStop = true;
					newLabel.FlatStyle = FlatStyle.System;

					// add the label
					this.Controls.Add(newLabel);
				}
			}
		}

		private void LinkLabel_Click(object sender, System.EventArgs e)
		{
			// re-raise the click event with our own parameter
			LinkLabel link = (LinkLabel) sender;
			
			// In C#, need to explicitly check that there is an event handler set.
			if (HistoryLinkClicked != null)
				HistoryLinkClicked(m_SelectedTaskID, (int) link.Tag);
		}

		public TaskHistoryPanel()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
	}
}
